package com.heeexy.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.service.GoodsService;
import com.heeexy.example.util.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
   
    
    //商品列表
    @RequiresPermissions("goods:list")
    @GetMapping("/list")
    public JSONObject list(HttpServletRequest request ) {
        return goodsService.list(CommonUtil.request2Json(request));
    }

   //添加商品
    @RequiresPermissions("goods:add")
    @PostMapping("/add")
    public JSONObject addGoods(@RequestBody JSONObject requestJson) {
    	 CommonUtil.hasAllRequired(requestJson, "gid,gname,gphoto,gtypeid,gbrandid,gdetail,gsell,gprice");
         return goodsService.add(requestJson);
    }

  //修改商品
    @RequiresPermissions("goods:update")
    @PostMapping("/update")
    public JSONObject updateGoods(@RequestBody JSONObject requestJson) {
    	CommonUtil.hasAllRequired(requestJson, "gid,gname,gphoto,gtypeid,gbrandid,gdetail,gsell,gprice");
        return goodsService.update(requestJson);

    }
    //删除商品
    @RequiresPermissions("goods:delete")
    @PostMapping("/delete")
    public  JSONObject deleteGoods(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson,"gid");
    	return goodsService.delete(requestJson);
    }
    
   
}
